﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_usergroup_detail : SystemPage.UIPage
    {
        private tcdc_mst_usergroup_detail objUsergroupDetail;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.UsergroupDetailAdd1.GridStore = StoreUsergroupDetail;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objUsergroupDetail = new tcdc_mst_usergroup_detail();
            objUsergroupDetail.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreUsergroupDetail.DataSource = GetUsergroupDetail();
                StoreUsergroupDetail.DataBind();
            }
        }

        protected DataTable GetUsergroupDetail()
        {
            DataTable dt = objUsergroupDetail.getByObject(new tcdc_mst_usergroup_detail());
            return dt;
        }

        protected void AddUsergroupDetail(object sender, DirectEventArgs e)
        {
            UsergroupDetailAdd1.Show();
        }

        protected void UpdateUsergroupDetail(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["UsergroupDetailValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string group_detail_id = string.Empty;
            string usergroup_id = string.Empty;
            string user_id = string.Empty;
            string begin_date = string.Empty;
            string end_date = string.Empty;
            string create_date = string.Empty;
            string create_by = string.Empty;
            string update_date = string.Empty;
            string update_by = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                group_detail_id = row["group_detail_id"].ToString();
                usergroup_id = row["usergroup_id"].ToString();
                user_id = row["user_id"].ToString();
                begin_date = row["begin_date"].ToString();
                end_date = row["end_date"].ToString();
                create_date = row["create_date"].ToString();
                create_by = row["create_by"].ToString();
                update_date = row["update_date"].ToString();
                update_by = row["update_by"].ToString();
            }

            objUsergroupDetail.group_detail_id = Convert.ToInt32(group_detail_id);
            objUsergroupDetail.usergroup_id = Convert.ToInt32(usergroup_id);
            objUsergroupDetail.user_id = Convert.ToInt32(user_id);
            objUsergroupDetail.begin_date = begin_date;
            objUsergroupDetail.end_date = end_date;
            objUsergroupDetail.create_date = create_date;
            objUsergroupDetail.create_by = Convert.ToInt32(create_by);
            objUsergroupDetail.update_date = update_date;
            objUsergroupDetail.update_by = Convert.ToInt32(update_by);
            objUsergroupDetail.updateData(objUsergroupDetail);
            StoreUsergroupDetail.DataSource = GetUsergroupDetail();
            StoreUsergroupDetail.DataBind();
        }

        protected void DeleteUsergroupDetail(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteUsergroupDetail();
        }

        protected void StoreUsergroupDetail_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreUsergroupDetail.DataSource = GetUsergroupDetail();
            StoreUsergroupDetail.DataBind();
        }

        protected void ExportExcelUsergroupDetail(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=UsergroupDetail.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteUsergroupDetail()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataUsergroupDetail()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataUsergroupDetail()
        {
            RowSelectionModel sm = this.GridPanelUsergroupDetail.SelectionModel.Primary as RowSelectionModel;
            string group_detail_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                group_detail_id = row.RecordID;
            }
            objUsergroupDetail.deleteData(group_detail_id);
            StoreUsergroupDetail.DataSource = GetUsergroupDetail();
            StoreUsergroupDetail.DataBind();
        }
    }
}

